function coverDOM(origin, container) {
    let ratio_origin = origin[0] / origin[1];
    let ratio_container = container[0] / container[1];
    // 按照当前的尺寸直接压缩 会失真
    let ratio_target = [container[0] / origin[0], container[1] / origin[1]];
    // 依据高度计算 如果超出容器 说明原先的压缩比 
    if (ratio_target[0] * origin[1] < container[1]) {
        // 高度失真 矫正高度压缩
        ratio_target[0] = ratio_target[1];
    } else if (ratio_target[1] * origin[0] < container[0]) {
        // 宽度失真 矫正宽度压缩
        ratio_target[1] = ratio_target[0];
    }
    return {
        scale: ratio_target,
        target: [origin[0] * ratio_target[0], origin[1] * ratio_target[1]],
    }
}

function checkCover(){
    let main = document.querySelector('[data-cover-main]');
    let origin = document.querySelector('[data-cover-origin]');
    let originMatrix = JSON.parse(origin.getAttribute('data-cover-origin'));
    let containerMatrix = JSON.parse(origin.getAttribute('data-cover-main')) || [0, 0];
    origin.setAttribute('width', originMatrix[0]);
    origin.setAttribute('height', originMatrix[1]);

    function onResize() {
        let coverMatrix = coverDOM(originMatrix, [containerMatrix[0] || window.innerWidth, containerMatrix[1] || window.innerHeight]);
        let scale = coverMatrix.scale.join(',');
        main.style.transformOrigin = '0 0';
        main.style.transform = 'scale(' + scale + ')';
    }
    window.addEventListener('resize', onResize);
    onResize();
}

checkCover();
